package com.imooc.article.controller;

import com.imooc.pojo.Article;
import com.imooc.pojo.Spouse;
import com.imooc.pojo.Stu;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.Version;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 *
 * @auther Levi
 * @Date: 2021/03/22/13:50
 * @Description:
 */
@Controller
@RequestMapping("/free")
public class FreemarkerController {

    @Value("${freemarker.html.path}")
    private String htmlTarget;


    @RequestMapping("/createHtml")
    @ResponseBody
    public String createHtml(Model model) throws Exception{
        //1.配置freemarker基本环境
        Configuration cfg = new Configuration(Configuration.getVersion());
        String classpath = this.getClass().getResource("/").getPath();
        cfg.setDirectoryForTemplateLoading(new File(classpath + "templates"));
        System.out.println(htmlTarget);
        System.out.println(classpath + "/templates");
        //2.获得现有的ftl模板
        Template cfgTemplate = cfg.getTemplate("stu.ftl", "utf-8");
        //3.获得动态数据
        model.addAttribute("word", "word");
        makeModel(model);
        //4.融合动态数据和ftl
        File filepath = new File(htmlTarget);
        if (!filepath.exists()) {
            filepath.mkdirs();
        }
        File file = new File(htmlTarget+File.separator+"1001.html");
        Writer out = new FileWriter(file);
        cfgTemplate.process(model, out);
        return "ok";
    }


    @GetMapping("/hello")
    public Object hello(Model model) {

        model.addAttribute("word", "word");
        makeModel(model);

        return "stu";
    }

    private Model makeModel(Model model) {
        Stu stu = new Stu();
        stu.setUid("100001");
        stu.setUsername("levi");
        stu.setAge(25);
        stu.setAmount(99999999999.9f);
        stu.setBirthday(new Date());
        stu.setHaveChild(false);

        Spouse spouse = new Spouse();
        spouse.setName("Lucy");
        spouse.setAge(25);
        stu.setSpouse(spouse);

        stu.setArticleList(getArticleList());
        stu.setParents(getParents());
        model.addAttribute("stu", stu);
        return model;
    }

    private List<Article> getArticleList() {

        Article article = new Article();
        article.setId("1001");
        article.setTitle("title1");

        Article article1 = new Article();
        article1.setId("1002");
        article1.setTitle("title2");

        Article article2 = new Article();
        article2.setId("1003");
        article2.setTitle("title3");

        List<Article> list = new ArrayList<>();
        list.add(article);
        list.add(article1);
        list.add(article2);
        return list;
    }

    private Map<String, String> getParents() {
        Map<String, String> map = new HashMap<>();
        map.put("father", "Tom");
        map.put("mother", "Lucy");
        return map;
    }

}
